import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login'
import store from './store'
import { setAuth } from './http'
import NotFound from '@/components/NotFound'
Vue.use(VueRouter)


const routes = [
    {
        path: '/',
        redirect: '/index',
    },
    {
        path: '/index',
        name: 'index',
        component: () => import(/* webpackChunkName: "index" */ '@/components/Index'),
        // children: funcRoutes,
        meta: {
            requireAuth: true
        }
    },

    {
        path: '/yunjian',
        name: 'yunjian',
        component: () => import(/* webpackChunkName: "YunJian" */ '@/components/YunJian'),
        meta: {
            requireAuth: true
        }
    },

    {
        path: '/jiance',
        name: 'jiance',
        component: () => import(/* webpackChunkName: "ChuNengJianCe" */ '@/components/ChuNengJianCe'),
        meta: {
            requireAuth: true
        }
    },

    {
        path: '/zxjc',
        name: 'zxjc',
        component: () => import(/* webpackChunkName: "zxjc" */ '@/components/ZXJC'),
        // children: funcRoutes,
        meta: {
            requireAuth: true
        }
    }, {
        path: '/dcc',
        name: 'dcc',
        component: () => import(/* webpackChunkName: "dcc" */ '@/components/dcc'),
        // children: funcRoutes,
        meta: {
            requireAuth: true
        }
    }, {
        path: '/dtdc',
        name: 'dtdc',
        component: () => import(/* webpackChunkName: "dtdc" */ '@/components/dtdc'),
        // children: funcRoutes,
        meta: {
            requireAuth: true
        }
    }, {
        path: '/pcs',
        name: 'pcs',
        component: () => import(/* webpackChunkName: "pcs" */ '@/components/pcs'),
        // children: funcRoutes,
        meta: {
            requireAuth: true
        }
    }, {
        path: '/zxgz',
        name: 'zxgz',
        component: () => import(/* webpackChunkName: "zxgz" */ '@/components/zxgz'),
        // children: funcRoutes,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/ztpg',
        name: 'ztpg',
        component: () => import(/* webpackChunkName: "ztpg" */ '@/components/ztpg'),
        // children: funcRoutes,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/404',
        name: '404',
        component: NotFound
    },
    {
        path: '*',
        redirect: '/404'
    }

]


if (sessionStorage.getItem("store")) {
    // console.log(sessionStorage.getItem("store"))
    store.replaceState(Object.assign({}, store.state, JSON.parse(sessionStorage.getItem("store"))))
    if (store.state.token) {
        setAuth(store.state.token);
    }
}

const router = new VueRouter({
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
        // console.log(to.fullPath);
        if (store.state.login) {
            next()
        } else {
            next({
                path: '/login', query: {
                    redirect: to.fullPath
                }
            })
        }
    }
    else {
        next()
    }
})

export default router